package com.braveman.im.ui.weiget;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;

import com.braveman.im.R;
import com.braveman.im.bean.FriendGroupNameBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.AddFriendsGroupNameActivity;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;

import de.greenrobot.event.EventBus;
import razerdp.basepopup.BasePopupWindow;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by 大灯泡 on 2016/1/15.
 * 从底部滑上来的popup
 */
public class DeleteFromBottomPopup extends BasePopupWindow implements View.OnClickListener {
    private Activity context;
    private View popupView;
    private String id;

    public DeleteFromBottomPopup(Activity context,String id) {
        super(context);
        bindEvent();
        this.context = context;
        this.id = id;
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_slide_from_detele, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            popupView.findViewById(R.id.tx_1).setOnClickListener(this);
            popupView.findViewById(R.id.tx_2).setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tx_1://删除指定分组
                doDeteleNetWork(id);
                DeleteFromBottomPopup.this.dismiss();
                break;
            case R.id.tx_2://取消
                DeleteFromBottomPopup.this.dismiss();
                break;
            default:
                break;
        }

    }

    private void doDeteleNetWork(String id) {
        ApiManager.getApi().deleteFriendGroupName(id).enqueue(new NetCallBack<FriendGroupNameBean>() {
            @Override
            protected void onSuccess(Call<FriendGroupNameBean> call, Response<FriendGroupNameBean> response) {
                if (response != null) {
                    UIUtils.showToast("删除分组成功");
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_DELETE_GROUP_NAME));
                }
            }
        });
    }

}
